﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DX_DailyUtil_1._0._1.ks.util
{
    public class Util_Calc
    {
        /// <summary>
        /// 分解字符串中括号左、中、右部分
        /// </summary>
        /// <param name="AText">所要分析的字符串</param>
        /// <param name="ALStr">输出左边</param>
        /// <param name="ACStr">输出中间</param>
        /// <param name="ARStr">输出右边</param>
        public static void Bracket(string AText,
            out string ALStr, out string ACStr, out string ARStr)
        {
            int L = 0, R = 0; // 出现的括号，判断匹配用
            ALStr = ACStr = ARStr = "";
            for (int i = 0; i < AText.Length; i++)
            {
                if (AText[i] == '(') L++;
                else if (AText[i] == ')') R++;
                if (L == 0) ALStr += AText[i];
                else if (L > R) ACStr += AText[i];
                else
                {
                    ARStr = AText.Substring(i + 1);
                    break;
                }
            }
            if (ACStr.Length >= 1) ACStr = ACStr.Remove(0, 1);
        }

        public static string fCalc(string AText)
        {
            if (AText.IndexOf('(') >= 0)
            {
                string L, C, R;
                Bracket(AText, out L, out C, out R);
                return fCalc(L + fCalc(C) + R);
            }
            else if ((AText.IndexOf('+') >= 0) || (AText.IndexOf('-') >= 0))
            {
                int i = AText.IndexOf('+');
                int j = AText.IndexOf('-');
                if (i < 0) i = AText.Length - 1;
                if (j < 0) j = AText.Length - 1;
                int K = Math.Min(i, j);
                string L = AText.Substring(0, K);
                string R = AText.Substring(K + 1);
                if (L.Length <= 0) L = "0";
                if (R.Length <= 0) R = "0";
                if (i == K)
                    return (double.Parse(fCalc(L)) + double.Parse(fCalc(R))).ToString();
                else return (double.Parse(fCalc(L)) - double.Parse(fCalc(R))).ToString();
            }
            else if ((AText.IndexOf('*') >= 0) || (AText.IndexOf('/') >= 0))
            {
                int i = AText.IndexOf('*');
                int j = AText.IndexOf('/');
                if (i < 0) i = AText.Length - 1;
                if (j < 0) j = AText.Length - 1;
                int K = Math.Min(i, j);
                string L = AText.Substring(0, K);
                string R = AText.Substring(K + 1);
                if (L.Length <= 0) L = "0";
                if (R.Length <= 0) R = "0";
                if (i == K)
                    return (double.Parse(fCalc(L)) * double.Parse(fCalc(R))).ToString();
                else return (double.Parse(fCalc(L)) / double.Parse(fCalc(R))).ToString();
            }
            else if ((AText.IndexOf('_') >= 0))
                return (-double.Parse(fCalc(AText.Substring(1)))).ToString();
            else return double.Parse(AText).ToString();
        }
        /// <summary>
        /// 计算表达式
        /// </summary>
        /// <param name="AText">输入的表达式</param>
        /// <returns>返回计算的结果</returns>
        public static string Calc(string AText)
        {
            string vText = "";
            int L = AText.Length;
            #region 处理负号
            for (int i = 0; i < L; i++)
            {
                if ((AText[i] == '-') && (i + 1 < L) &&
                    ("+-()".IndexOf(AText[i + 1]) < 0))
                {
                    if ((i == 0) || ((i > 0) && ("*/".IndexOf(AText[i - 1]) >= 0)))
                        vText += "_";
                    else if (((i > 0) && ("+-".IndexOf(AText[i - 1]) >= 0)) ||
                        ((i + 1 < L) && ("+-()".IndexOf(AText[i + 1]) < 0)))
                        vText += "+_";
                    else vText += AText[i];
                }
                else vText += AText[i];
            }
            #endregion
            return fCalc(vText);
        }

    }
}
